﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EthernetTools.UI
{
    public partial class HexDataModyfiingDialog : Form
    {
        public byte[] HexValues;

        private bool supressDGevents = false;
            
        public HexDataModyfiingDialog(string caption, byte[] modyfingValue)
        {
            InitializeComponent();

            this.Text += caption;
            if (modyfingValue != null)
            {
                HexValues = new byte[modyfingValue.Length - 4];
                for (int i = 4, j = 0; i < modyfingValue.Length; ++j, ++i)
                {
                    HexValues[j] = modyfingValue[i];
                }
            }
            else
            {
                HexValues = new byte[0];
            }

            label_resultLength.Text = "Result length:" + HexValues.Length.ToString("X2");

            int bIndexer = 0;
            dataGridView1.Rows.Clear();

            dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;

            dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
           
            if (HexValues.Length > 0)
            {
                for (int i = 0; i < (HexValues.Length / 8) + 1; ++i )
                {
                    dataGridView1.Rows.Add();

                    for (int j = 0; j < 8; ++j )
                    {
                        dataGridView1.Rows[i].Cells[j].Value = HexValues[bIndexer].ToString("X2");

                        ++bIndexer;
                        if (bIndexer >= HexValues.Length)
                            break;
                    }

                    if (bIndexer > HexValues.Length)
                        break;
                }
            }

        }
        
        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (supressDGevents)
                return;
            string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

            byte[] cellbyte = Utils.ByteUtils.ParseBytes(value);

            if (cellbyte == null)
            {
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null;
                return;
            }
            if (cellbyte.Length == 0)
            {
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null;
                return;
            }

            int arrayIndexer = e.RowIndex * 8 + e.ColumnIndex;
            if (arrayIndexer >= HexValues.Length)
            {
                byte[] append = new byte[arrayIndexer - HexValues.Length + 1];
                Utils.ByteUtils.AppendBytes(ref HexValues, append);
            }

            HexValues[arrayIndexer] = cellbyte[0];
        }

        void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (supressDGevents)
                return;

            int i = e.RowIndex;
            int j = e.ColumnIndex;

            while (true)
            {
                if (j > 0)
                {
                    if (dataGridView1.Rows[i].Cells[j - 1].Value == null)
                    {
                        dataGridView1.Rows[i].Cells[j - 1].Value = "00";
                        --j;
                    }
                    else
                    {
                        break;
                    }
                    
                }
                else
                {
                    if (i > 0)
                    {
                        if (dataGridView1.Rows[i-1].Cells[7].Value == null)
                        {
                            dataGridView1.Rows[i - 1].Cells[7].Value = "00";
                            j = 7;
                            --i;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
            /* 
            supressDGevents = true;
            dataGridView1.EndEdit();
            dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[j];

            supressDGevents = false;
             * */
        }

        private void ButtonOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
